package com.huike.web.controller.system;

import com.huike.clues.domain.SysDictType;
import com.huike.clues.domain.dto.my.DictExportDTO;
import com.huike.clues.domain.dto.my.DictQueryDTO;
import com.huike.clues.service.SysDictTypeService;
import com.huike.common.annotation.Log;
import com.huike.common.annotation.RepeatSubmit;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictTypeDTO;
import com.huike.common.core.domain.entity.SysUserDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * 字典类型 控制层
 */
@Slf4j
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    /**
     * 新增字典数据
     *
     * @param sysDictTypeDTO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "字典管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult addDictData(@RequestBody SysDictTypeDTO sysDictTypeDTO) {
        log.info("新增字典数据:{}", sysDictTypeDTO);
        return toAjax(sysDictTypeService.addDictData(sysDictTypeDTO));
    }


    /**
     * 修改字典类型
     */
    @PutMapping
    public AjaxResult updateDictType(@RequestBody SysDictTypeDTO sysDictTypeDTO) {
        log.info("修改数据类型:{}", sysDictTypeDTO);
        return toAjax(sysDictTypeService.updateDictData(sysDictTypeDTO));
    }

    /**
     * 清空缓存数据
     *
     * @param
     * @return
     */
    @DeleteMapping("/clearCache")
    public AjaxResult clearCache() {
        log.info("清空缓存:");
        sysDictTypeService.clearCache();
        return AjaxResult.success();
    }

    /**
     * 分页查询数据字典类型信息
     */
    @GetMapping("/list")
    public TableDataInfo list(DictQueryDTO dictQueryDTO) {
        startPage();
        List<SysDictType> list = sysDictTypeService.selectSysDictTypeList(dictQueryDTO);
        return getDataTable(list);
    }

    /**
     * 导出字典数据类型
     *
     * @param dictExportDTO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @Log(title = "字典管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DictExportDTO dictExportDTO) {
        List<DictExportDTO> list = sysDictTypeService.selectUserList(dictExportDTO);
        ExcelUtil<DictExportDTO> util = new ExcelUtil<DictExportDTO>(DictExportDTO.class);
        return util.exportExcel(list, "字典类型数据");
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<SysDictType> list = sysDictTypeService.optionselect();
        return AjaxResult.success(list);
    }

    /**
     * 删除字典类型
     *
     * @param dictIds
     * @return
     */
    @DeleteMapping("/{dictIds}")
    public AjaxResult delectDictType(@PathVariable Long[] dictIds) {
        return toAjax(sysDictTypeService.delectDictType(dictIds));
    }

    /**
     * 查询字典类型详细信息
     *
     * @return
     */
    @GetMapping("/{dictId}")
    public AjaxResult selectDictType(@PathVariable Long dictId) {
        SysDictType dictType = sysDictTypeService.selectDictType(dictId);
        return AjaxResult.success(dictType);
    }
}
